package com.dongdongshop.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdongshop.annotation.Log;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationExample;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.pojo.TbSpecificationOptionExample;
import com.dongdongshop.service.TbSpecificationService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
@org.springframework.stereotype.Service
public class TbSpecificationServiceImpl implements TbSpecificationService {

    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;

    @Override
    @Log
    public List<TbSpecification> getSpecInfo(String specName) {
        return tbSpecificationMapper.selectByName(specName);
    }

    @Override
    @Log
    public void insertSpecInfo(TbSpecification tbSpecification, List<TbSpecificationOption> options) {
        // 增加返回主键 id
        tbSpecificationMapper.insertById(tbSpecification);
//        Long id = tbSpecification.getId();
        for (TbSpecificationOption option : options) {
            option.setSpecId(tbSpecification.getId());
            // 增加规格表
            tbSpecificationOptionMapper.insertSelective(option);
        }
        // lambda 写法格式
//        options.stream().forEach(option -> option.setSpecId(tbSpecification.getId()));
    }

    @Override
    @Log
    public TbSpecification updateSpec(Long id) {
        return tbSpecificationMapper.selectByPrimaryKey(id);
    }

    @Override
    @Log
    public List<TbSpecificationOption> selectBySpecOp(Long id) {
        TbSpecificationOptionExample toe = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = toe.createCriteria();
        criteria.andSpecIdEqualTo(id);
        return tbSpecificationOptionMapper.selectByExample(toe);
    }

    @Override
    @Log
    public void updateSpecInfo(TbSpecification tbSpecification, List<TbSpecificationOption> options) {
        // 删除中间表
        TbSpecificationOptionExample toe = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = toe.createCriteria();
        criteria.andSpecIdEqualTo(tbSpecification.getId());
        tbSpecificationOptionMapper.deleteByExample(toe);
        // 修改 名称表
        tbSpecificationMapper.updateByPrimaryKeySelective(tbSpecification);
        // 增加
        for (TbSpecificationOption option : options) {
            option.setSpecId(tbSpecification.getId());
            // 增加规格表
            tbSpecificationOptionMapper.insertSelective(option);
        }
    }

    @Override
    @Log
    public void deleteSpecInfo(Long[] ids) {
        // 删除 名称表
        tbSpecificationMapper.deleteBySpecId(ids);
        // 删除 规格表
        for (int i = 0; i < ids.length; i++) {
            TbSpecificationOptionExample toe = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = toe.createCriteria();
            criteria.andSpecIdEqualTo(ids[i].longValue());
            tbSpecificationOptionMapper.deleteByExample(toe);
        }
    }
}
